/*
 * Copyright (C) 2009 Ionut Dediu <deionut@yahoo.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// SmartCamViewFinderContainer.cpp

#include <SmartCam_0xE696FBC9.rsg>

#include "SmartCamViewFinderContainer.h"
#include "SmartCamAppUi.h"

CSmartCamViewFinderContainer::CSmartCamViewFinderContainer()
    {
	iSmartCamAppUi = static_cast<CSmartCamAppUi*> (CEikonEnv::Static()->EikAppUi());
    }

CSmartCamViewFinderContainer* CSmartCamViewFinderContainer::NewL(const TRect& aRect)
    {
    CSmartCamViewFinderContainer* self = CSmartCamViewFinderContainer::NewLC(aRect);
    CleanupStack::Pop(self);
    return self;
    }

CSmartCamViewFinderContainer* CSmartCamViewFinderContainer::NewLC(const TRect& aRect)
    {
    CSmartCamViewFinderContainer* self = new (ELeave) CSmartCamViewFinderContainer();
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    return self;
    }

void CSmartCamViewFinderContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();
    // Set the windows size
    SetRect(aRect);
    // note, parent view will activate this container
    }

CSmartCamViewFinderContainer::~CSmartCamViewFinderContainer()
    {
    }

TInt CSmartCamViewFinderContainer::CountComponentControls() const
    {
    return 0;
    }

CCoeControl* CSmartCamViewFinderContainer::ComponentControl(TInt /* aIndex */ ) const
    {
    return NULL;
    }

TKeyResponse CSmartCamViewFinderContainer::OfferKeyEventL(const TKeyEvent& /* aKeyEvent */, TEventCode /* aType */)
    {
	return EKeyWasNotConsumed;
    }

void CSmartCamViewFinderContainer::Draw(const TRect& aRect) const
	{
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor(TRgb(255, 255, 255));	// white
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(aRect);
    CFbsBitmap* bitmap = NULL;
    
    if(iSmartCamAppUi->IsConnected())
    	{
    	bitmap = iSmartCamAppUi->ViewFinderFrame();
    	}
    else
    	{
    	bitmap = iSmartCamAppUi->LogoBmp();
    	}
	// draw the current bmp (if exists)
	if(bitmap != NULL)
		{
		// position the drawn bitmap in the center
		TPoint topLeft(
				(Rect().Width() - bitmap->SizeInPixels().iWidth) / 2,
				(Rect().Height() - bitmap->SizeInPixels().iHeight) / 2);
		gc.BitBlt(topLeft, bitmap);
		}
	}
